package com.hub.realtime.resource;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hub.realtime.resource.dto.input.CatalogQueryParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hub.realtime.common.annotation.Log;
import com.hub.realtime.common.core.controller.BaseController;
import com.hub.realtime.common.core.domain.AjaxResult;
import com.hub.realtime.common.enums.BusinessType;
import com.hub.realtime.resource.domain.ResCatalog;
import com.hub.realtime.resource.service.IResCatalogService;
import com.hub.realtime.common.utils.poi.ExcelUtil;
import com.hub.realtime.common.core.page.TableDataInfo;

/**
 * 元数据中心Controller
 *
 * @author wupeng
 * @date 2022-02-09
 */
@RestController
@RequestMapping("/resource/catalog")
public class ResCatalogController extends BaseController {
    @Autowired
    private IResCatalogService resCatalogService;

    /**
     * 查询元数据中心列表
     */
    @PreAuthorize("@ss.hasPermi('resource:catalog:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatalogQueryParam param) {
        Page<ResCatalog> page = resCatalogService.list(param);
        return getDataTable(page.getRecords(), page.getTotal());
    }

    @GetMapping("/allList")
    public AjaxResult allList() {
        List<ResCatalog>list= resCatalogService.getCatalogList();
        return AjaxResult.success(list);
    }

    /**
     * 导出元数据中心列表
     */
    @PreAuthorize("@ss.hasPermi('resource:catalog:export')")
    @Log(title = "元数据中心", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CatalogQueryParam param) {
        List<ResCatalog> list = resCatalogService.list(param).getRecords();
        ExcelUtil<ResCatalog> util = new ExcelUtil<ResCatalog>(ResCatalog.class);
        util.exportExcel(response, list, "元数据中心数据");
    }

    /**
     * 获取元数据中心详细信息
     */
    @PreAuthorize("@ss.hasPermi('resource:catalog:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(resCatalogService.getBaseMapper().selectById(id));
    }

    /**
     * 新增元数据中心
     */
    @PreAuthorize("@ss.hasPermi('resource:catalog:add')")
    @Log(title = "元数据中心", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResCatalog resCatalog) {
        resCatalogService.addCatalog(resCatalog);
        return AjaxResult.success("添加成功！");
    }

    /**
     * 修改元数据中心
     */
    @PreAuthorize("@ss.hasPermi('resource:catalog:edit')")
    @Log(title = "元数据中心", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResCatalog resCatalog) {
        resCatalogService.updateCatalog(resCatalog);
        return AjaxResult.success("修改成功！");
    }

    /**
     * 删除元数据中心
     */
    @PreAuthorize("@ss.hasPermi('resource:catalog:remove')")
    @Log(title = "元数据中心", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(resCatalogService.deleteInfos(ids));
    }

    @PostMapping("/testCatalog")
    public AjaxResult testCatalog(@RequestBody ResCatalog resCatalog) {
        resCatalogService.testCatalog(resCatalog);
        return AjaxResult.success("测试成功！");
    }

}
